package com.iwhalecloud.bss.kite.cucc.service.attr.linkhandler;

import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.service.attr.handler.*;
import com.iwhalecloud.bss.kite.dataservice.cache.AttrValueCache;
import com.iwhalecloud.bss.kite.dataservice.cache.ProdAttrCache;
import com.iwhalecloud.bss.kite.dataservice.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.ProdAttr;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandler;
import com.iwhalecloud.bss.kite.listener.handler.attr.annotation.AttrHandlers;
import com.iwhalecloud.bss.kite.listener.handler.attr.impl.AbstractAttrLinkageHandler;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteInst;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.iwhalecloud.bss.kite.service.attr.AttrData;
import org.apache.commons.collections.ListUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author xueyf
 * @Description
 * @Date 2021/5/25 19:57
 */
@Service
@AttrHandlers({ @AttrHandler(CircuitTypeHandler.class), @AttrHandler(BandwidthBusinessModelHandler.class), @AttrHandler(InternetBelongHandler.class),
    @AttrHandler(BasedBandwidthHandler.class),@AttrHandler(FixedBandwidthHandler.class),@AttrHandler(SuddenBandwidthHandler.class)})
public class InterfaceTypeAZLinkHandler extends AbstractAttrLinkageHandler<AttrData> {

    /**
     * 承载电路类型
     */
    private static final String CIRCUIT_TYPE = "210004190";
    /**
     * 以太网类型
     */
    private static final String ETHERNET= "110007436";
    /**
     * 数字电路
     */
    private static final String DIGITAL_CIRCUIT = "1_0000229";
    /**
     * 业务带宽模式
     */
    private static final String BANDWITCH_MODE = "210004221";
    /**
     * 可变带宽模式
     */
    private static final String CHANGE_BANDWITCH_MODE = "110007227";
    /**
     * 固定带宽模式
     */
    private static final String UNCHANGE_BANDWITCH_MODE = "110007226";
    /**
     * 网络归属
     */
    private static final String INTER_BELONG = "210004441";

    /**
     * 智能OTN
     */
    private static final String INTELLECT_OTN = "110007434";

    /**
     * 普通OTN
     */
    private static final String COMMON_OTN = "110007435";

    /**
     * A端接口类型
     */
    private static final String INTERFACE_TYPE_A = "210004203";
    /**
     * Z端接口类型
     */
    private static final String INTERFACE_TYPE_Z = "210004212";
    /**
     * 固定带宽
     */
    private static final String FIXED_BAND_WIDTH = "210004224";
    /**
     * 基础带宽
     */
    private static final String BASE_BAND_WIDTH = "200001818";
    /**
     * 突发带宽
     */
    private static final String SUDDEN_BAND_WIDTH = "210004191";

    private static final String VALUE_110015538 = "110015538"; //(OTU1)
    private static final String VALUE_110015539 = "110015539"; //(OTU2)
    private static final String VALUE_110015540 = "110015540"; //(OTU2e)
    private static final String VALUE_100004844 = "100004844"; //(FE光)
    private static final String VALUE_100004845 = "100004845"; //(FE电)
    private static final String VALUE_100007382 = "100007382"; //(100GE)
    private static final String VALUE_110007234 = "110007234"; //(STM-1电)
    private static final String VALUE_110007238 = "110007238"; //(10GE LAN)
    private static final String VALUE_110007237 = "110007237"; //(STM-64光)
    private static final String VALUE_110007236 = "110007236"; //(STM-16光)
    private static final String VALUE_110007235 = "110007235"; //(STM-4光)
    private static final String VALUE_110007239 = "110007239"; //(10GE WAN)
    private static final String VALUE_110007240 = "110007240"; //(STM-1光)
    private static final String VALUE_110007274 = "110007274"; //(G.703电)
    private static final String VALUE_110007276 = "110007276"; //(G.703光)
    private static final String VALUE_1_0000201 = "1_0000201"; //(GE光口)
    private static final String VALUE_1_0000202 = "1_0000202"; //(GE电口)
    private static final String VALUE_1_0000409 = "1_0000409"; //(GE)

    @Override
    public void handle(KiteAttr attr, KiteInst inst, AttrData attrData) {

        KiteAttr interfaceTypeAttrA = inst.getAttr(INTERFACE_TYPE_A);
        KiteAttr interfaceTypeAttrZ = inst.getAttr(INTERFACE_TYPE_Z);
        String attrId = interfaceTypeAttrA.getAttrId();
        List<AttrValue> attrValues = AttrValueCache.get(attrId);
        List<KiteAttrValue> kiteAttrValues = new ArrayList<>();
        attrValues.forEach(attrValue -> {
            KiteAttrValue kiteAttrValue = new KiteAttrValue();
            BeanUtils.copyProperties(attrValue,kiteAttrValue);
            kiteAttrValues.add(kiteAttrValue);
        });
        KiteAttr circuitTypeAttr = inst.getAttr(CIRCUIT_TYPE);
        KiteAttr bandwitchModeAttr = inst.getAttr(BANDWITCH_MODE);
        KiteAttr interBelongAttr = inst.getAttr(INTER_BELONG);
        KiteAttr fixedBandWidthAttr = inst.getAttr(FIXED_BAND_WIDTH);
        KiteAttr baseBandWidthAttr = inst.getAttr(BASE_BAND_WIDTH);
        KiteAttr suddenBandWidthAttr = inst.getAttr(SUDDEN_BAND_WIDTH);
        String circuitType = circuitTypeAttr.getValue();
        String bandwitchMode = bandwitchModeAttr.getValue();
        String interBelong = interBelongAttr.getValue();
        List<KiteAttrValue> kiteAttrValuesNew = new ArrayList<>();
        Map<String,List<String>> filterConfig = getFilterConfig();
        if(!StringUtils.isEmpty(interBelong)&&!StringUtils.isEmpty(bandwitchMode)&&!StringUtils.isEmpty(circuitType)&&kiteAttrValues!=null&&kiteAttrValues.size()>0){
            kiteAttrValuesNew = kiteAttrValues.stream().filter(kiteAttrValue -> {
                List<String> strings = filterConfig.get(kiteAttrValue.getAttrValue());
                boolean flag1 = false;
                boolean flag2 = false;
                boolean flag3 = false;
                boolean flag4 = false;
                if(KiteListUtils.isNotEmpty(strings)&& strings.size() == 4){
                    flag1 = "1".equals(strings.get(0)) || KiteStringUtils.equals(interBelong,strings.get(0));
                    flag2 = "1".equals(strings.get(1)) || KiteStringUtils.equals(circuitType,strings.get(1));
                    flag3 = "1".equals(strings.get(2)) || KiteStringUtils.equals(bandwitchMode,strings.get(2));
                    String bandWidth = strings.get(3);
                    if(LocalKeyConsts.KITE_ATTR_CONFIG_SHOW_TYPE_D.equals(fixedBandWidthAttr.getShowType())){
                        String fixedBandWidth = fixedBandWidthAttr.getValueDesc();
                        if(compareBandWidth(bandWidth,fixedBandWidth,null)>=0){
                            flag4 =true;
                        }
                    }else{
                        String baseBandWidth = baseBandWidthAttr.getValueDesc();
                        String suddenBandWidth = suddenBandWidthAttr.getValueDesc();
                        if(compareBandWidth(bandWidth,baseBandWidth,suddenBandWidth)>=0){
                            flag4 =true;
                        }
                    }
                }
                return flag1 && flag2 && flag3 && flag4;
            }).collect(Collectors.toList());
        }


        if(kiteAttrValuesNew.stream().anyMatch(kiteAttrValue -> KiteStringUtils.equals(kiteAttrValue.getAttrValue(), interfaceTypeAttrA.getValue()))){
            interfaceTypeAttrA.setAttrValues(kiteAttrValuesNew);
        }else{
            interfaceTypeAttrA.setValue(kiteAttrValuesNew.size()>0?kiteAttrValuesNew.get(0).getAttrValue():"");
            interfaceTypeAttrA.setValueDesc(kiteAttrValuesNew.size()>0?kiteAttrValuesNew.get(0).getAttrValueDesc():"");
            interfaceTypeAttrA.setAttrValues(kiteAttrValuesNew);
        }

        if(kiteAttrValuesNew.stream().anyMatch(kiteAttrValue -> KiteStringUtils.equals(kiteAttrValue.getAttrValue(), interfaceTypeAttrZ.getValue()))){
            interfaceTypeAttrZ.setAttrValues(kiteAttrValuesNew);
        }else{
            interfaceTypeAttrZ.setValue(kiteAttrValuesNew.size()>0?kiteAttrValuesNew.get(0).getAttrValue():"");
            interfaceTypeAttrZ.setValueDesc(kiteAttrValuesNew.size()>0?kiteAttrValuesNew.get(0).getAttrValueDesc():"");
            interfaceTypeAttrZ.setAttrValues(kiteAttrValuesNew);
        }

    }


    @Override
    public void handleStyle(KiteAttr kiteAttr, KiteInst kiteInst, AttrData attrData) {

    }

    private void getNewAttrList(List<KiteAttrValue> oldAttrList, List <String> configList, List<KiteAttrValue> newAttrList){
        configList.stream().forEach(configValue->{
            oldAttrList.stream().forEach(kiteAttrValue -> {
                if(KiteStringUtils.equals(kiteAttrValue.getAttrValue(),configValue)){
                    newAttrList.add(kiteAttrValue);
                }
            });
        });
    }

    private double compareBandWidth(String bandWidth, String bandWidth1,String bandWidth2){
        return getBandWith(bandWidth)-(getBandWith(bandWidth1)+getBandWith(bandWidth2));
    }

    private double getBandWith(String bandWidth){
        if(KiteStringUtils.isEmpty(bandWidth)){
            return 0;
        }
        if(bandWidth.contains("无")){
            return 0;
        }

        int originUnit = bandWidth.endsWith("G") ? 1024 : 1;
        double bandWidthNum = Double.parseDouble(bandWidth.replaceAll("[^0-9\\.]", "")) * originUnit;
        return bandWidthNum;
    }

    private Map<String,List<String>> getFilterConfig(){
        // key为接口类型枚举值，值为[是否智能、电路类型、带宽模式] ,1为都可以选择
        Map<String,List<String>> filterConfig = new HashMap<>(18);
        filterConfig.put(VALUE_110015538,new ArrayList<>(Arrays.asList(COMMON_OTN,DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"2.5G")));
        filterConfig.put(VALUE_110015539,new ArrayList<>(Arrays.asList(COMMON_OTN,DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"10G")));
        filterConfig.put(VALUE_110015540,new ArrayList<>(Arrays.asList(COMMON_OTN,DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"10G")));
        filterConfig.put(VALUE_100004844,new ArrayList<>(Arrays.asList("1",ETHERNET,UNCHANGE_BANDWITCH_MODE,"100M")));
        filterConfig.put(VALUE_100004845,new ArrayList<>(Arrays.asList("1",ETHERNET,UNCHANGE_BANDWITCH_MODE,"100M")));
        filterConfig.put(VALUE_100007382,new ArrayList<>(Arrays.asList("1",ETHERNET,"1","100G")));
        filterConfig.put(VALUE_110007234,new ArrayList<>(Arrays.asList("1",DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"155M")));
        filterConfig.put(VALUE_110007238,new ArrayList<>(Arrays.asList("1",ETHERNET,"1","10G")));
        filterConfig.put(VALUE_110007237,new ArrayList<>(Arrays.asList("1",DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"10G")));
        filterConfig.put(VALUE_110007236,new ArrayList<>(Arrays.asList("1",DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"2.5G")));
        filterConfig.put(VALUE_110007235,new ArrayList<>(Arrays.asList("1",DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"622M")));
        filterConfig.put(VALUE_110007239,new ArrayList<>(Arrays.asList("1",ETHERNET,"1","10G")));
        filterConfig.put(VALUE_110007240,new ArrayList<>(Arrays.asList("1",DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"155M")));
        filterConfig.put(VALUE_110007274,new ArrayList<>(Arrays.asList(COMMON_OTN,DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"2M")));
        filterConfig.put(VALUE_110007276,new ArrayList<>(Arrays.asList(COMMON_OTN,DIGITAL_CIRCUIT,UNCHANGE_BANDWITCH_MODE,"2M")));
        filterConfig.put(VALUE_1_0000201,new ArrayList<>(Arrays.asList("1",ETHERNET,"1","1G")));
        filterConfig.put(VALUE_1_0000202,new ArrayList<>(Arrays.asList("1",ETHERNET,"1","1G")));
        filterConfig.put(VALUE_1_0000409,new ArrayList<>(Arrays.asList("1",ETHERNET,"1","1G")));
        return filterConfig;
    }
}
